package com.xzz.cloud.alibaba.service.impl;

import com.xzz.cloud.alibaba.dao.OrderDao;
import com.xzz.cloud.alibaba.service.AccountService;
import com.xzz.cloud.alibaba.service.OrderService;
import com.xzz.cloud.alibaba.service.StorageService;
import com.xzz.cloud.entity.Order;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author: hhz
 * @createDate: 2021-4-6
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderDao orderDao;
    @Resource
    private StorageService storageservice;
    @Resource
    private AccountService accountservice;

    @Override
//    @GlobalTransactional(name = "create-order",rollbackFor = Exception.class)
    public void create(Order order) {
        log.info("----->开始新建订单");
        //1新建订单
        orderDao.create(order);
        log.info("----->订单微服务开始调用库存，做扣减count" );
        //2扣减库存
        storageservice.decrease(order);

        log.info("----->订单微服务开始调用库存，做扣减end" );

        log.info("----->订单微服务开始调用账户，做扣减Money" );
        //3扣减账户
        accountservice.decrease(order);
        log.info("----->订单微服务开始调用库存，做扣减end" ) ;
        //4修改订单状态，从零到到1,1代表已经完成
        log.info("----->修改订单状态开始");
        orderDao.update(order.getUserId(), 0);
        log.info("--------下订单结束了 = =！");
    }
}
